package JZOffer;

/**
 * @author Lw中
 * @date 2021/8/12 23:18
 */
public class Jz54 {

    // Jz54.二叉搜索树的第K大节点

    public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
    }

    int res, k;

    public int kthLargest(TreeNode root, int k) {
        this.k = k;
        dfs(root);
        return res;
    }

    /**
     * LWZ TODO : 2021/8/12 下面的dfs方法理解有点模糊
     *
     */
    void dfs(TreeNode root) {
        if (root == null) return;
        dfs(root.right);
        if (k == 0) return;
        if (--k == 0) res = root.val;
        dfs(root.left);
    }

}
